<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>generator&promise&Async/await</title>
</head>
<body>
<!--<script src="../jquery-1.3.1.js"></script>-->
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
<script>
    //场景: 用户登录成功之后,判断用户类型,如果是管理员,则显示所有用户信息
    //                              如果是普通用户,则显示当前的用户信息
    //ajax
    {
        /*$.ajax({
            url:"login?username='haha'&password='1234'",
            success(res){
                if(res.type === "manger"){
                    $.ajax({
                        url:"getAllUserInfo",
                        success(allUserInfo){
                            console.log("getAllUserInfo",allUserInfo)
                        }
                    })
                }else {
                    $.ajax({
                        url:"getCurrentUserInfo",
                        success(currentUserInfo){
                            console.log("getCurrentUserInfo",currentUserInfo)
                        }
                    })
                }
            }
        })*/
    }

    //generator
    {
        /*function myAjax(url){
            $.ajax({
                url:url,
                success(res){
                    console.log(url+"res===>",res)
                    it.next(res)
                }
            })
        }
        function * getData(){
            let data = yield myAjax("./aaa.txt");
            console.log(typeof data)
            if(JSON.parse(data).type === "manger"){
                let allUserInfo = yield myAjax("./bbb.txt");
                console.log("allUserInfo",allUserInfo)
            }else {
                let currentUserInfo = yield myAjax("./ccc.txt");
                console.log("currentUserInfo",currentUserInfo)
            }
        }
        let it = getData();
        it.next()*/
    }

    //promise
    {
       /* Promise.resolve(
            $.ajax({
                url:"./aaa.txt",
                success(res){
                    console.log(res);
                    if(JSON.parse(res).type === "manger"){
                        new Promise((resolve, reject)=>{
                            $.ajax({
                                url:"./bbb.txt",
                                success(allUserInfo){
                                    console.log("allUserInfo",allUserInfo)
                                }
                            })
                        })
                    }else {
                        new Promise((resolve, reject)=>{
                            $.ajax({
                                url:"./ccc.txt",
                                success(currentUserInfo){
                                    console.log("currentUserInfo",currentUserInfo)
                                }
                            })
                        })
                    }
                }
            })
        )*/
    }
    //async/await
    {
        /*async function getData (){
            let userType = await new Promise((resolve, reject)=>{
                $.ajax({
                    url:"./aaa.txt",
                    success(res){
                        resolve(res)
                    }
                })
            })
            console.log(userType);
            if(JSON.parse(userType).type === "manger"){
                let allUserInfo = await new Promise((resolve, reject)=>{
                    $.ajax({
                        url:"./bbb.txt",
                        success(allUserInfo){
                            resolve(allUserInfo)
                        }
                    })
                })
                console.log("allUserInfo",allUserInfo)
            }else {
                let currentUserInfo = await new Promise((resolve, reject)=>{
                    $.ajax({
                        url:"./ccc.txt",
                        success(currentUserInfo){
                            resolve(currentUserInfo)
                        }
                    })
                })
                console.log("currentUserInfo",currentUserInfo)
            }
        }
        getData();*/

        async function getData (){
            let userType = await Promise.resolve($.ajax("./aaa.txt"))
            console.log(userType);
            if(JSON.parse(userType).type === "manger"){
                let allUserInfo = await Promise.resolve($.ajax("./bbb.txt"))
                console.log("allUserInfo",allUserInfo)
            }else {
                let currentUserInfo = await Promise.resolve($.ajax("./ccc.txt"))
                console.log("currentUserInfo",currentUserInfo)
            }
        }
        getData();
    }

</script>
</body>
</html>